package com;

import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;

import jdk.nashorn.internal.objects.annotations.Where;

import conn.Cont;
import conn.ResultSetUtil;
import conn.StatementUtil;

public class Exam {
	
	@Test
	public void score() throws ClassNotFoundException, SQLException {
		int sscore=0;
		
		String sql1=" SELECT danxuan.ans FROM danxuan  ";
		Scanner sc=new Scanner(System.in);
		System.out.println("输入单选题答案");
		String ans1=sc.next();
		sscore+=new Exam().paper(ans1, sql1);
		System.out.println("单选择题得分"+new Exam().paper(ans1, sql1));
		
		String sql2=" SELECT duoxuan.ans FROM duoxuan  ";
		System.out.println("输入多选题答案");
		String ans2=sc.next();
		sscore+=new Exam().paper(ans1, sql1);
		System.out.println("多选择题得分"+new Exam().paper(ans2, sql2));
		
		String sql3=" SELECT tiankong.ans FROM tiankong  ";
		System.out.println("输入填空题答案");
		String ans3=sc.next();
		sscore+=new Exam().paper(ans1, sql1);
		System.out.println("填空题得分"+new Exam().paper(ans3, sql3));
		
		String sql4=" SELECT tiankong.ans FROM tiankong  ";
		System.out.println("输入判断题答案");
		String ans4=sc.next();
		sscore+=new Exam().paper(ans1, sql1);
		System.out.println("判断题得分"+new Exam().paper(ans4, sql4));
		
		System.out.println("总分为："+sscore);
		
	}
	
	/**
	 * 求分数函数
	 * @param ans
	 * @param sql
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public int paper(String ans,String sql) throws ClassNotFoundException, SQLException{
		Cont ct=new Cont();
		Connection conn=ct.getConnection();
		StatementUtil stam=new StatementUtil();
		Statement st=stam.getStatement(conn);
		ResultSetUtil rsu=new ResultSetUtil();
		
		ResultSet rs=rsu.getResult(st, sql);
		
		char[] a=ans.toCharArray();
		char[] b=null;
		System.out.println(a.length);
		 int score=0;
		boolean temp=false;
		
		while(rs.next()){
			String t=rs.getString(1);
			
			 b=t.toCharArray();
				 
			 }
		System.out.println(b.length);
			if(ans.equals(rs.getString(1))){
				score=score+10;
			}else if(a.length<b.length) {
				for(int i=0;i<b.length;i++) {
				for (int j = 0; j < a.length; j++) {
					if((temp=(b[j]==a[i]))==true){
						continue;
					}else{
						temp=false;
					}
				
			}
		}
				}
			if(temp==true){
				score=score+10/2;
			}else{
				score=score+0;
			}
			conn.close();
			st.close();
			rs.close();
			
			return score;
	}
	
	

}
